package com.bajiao.service.account.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bajiao.common.constant._Whole;
import com.bajiao.vo.account.AccountAdmin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bajiao.common.util.BCryptPasswordUtils;
import com.bajiao.common.util.DoubleUtil;
import com.bajiao.entity.account.Account;
import com.bajiao.entity.user.User;
import com.bajiao.mapper.account.AccountMapper;
import com.bajiao.mapper.user.UserMapper;
import com.bajiao.service.account.AccountBillService;
import com.bajiao.service.account.AccountService;
import com.bajiao.service.config.ConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 账户
 * @Author: FanJq
 * @Date: 2024-10-1005-11
 * @Version: V1.0
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Lazy
    @Resource
    private AccountBillService accountBillService;

    /**
     * @Description
     * 根据用户ID查询钱包/账户信息
     * @date 2024/12/12 10:24
     * @author FanJq
     * @param userId
     * @return {@link Account}
    */
    @Override
    public Account queryByUserId(String userId) {
        return super.lambdaQuery().eq(Account::getUserId, userId).one();
    }

    /**
     * @Description
     * 资金变动
     * @date 2024/12/12 10:11
     * @author FanJq
     * @param changeMoney 变化金额
     * @param billType 1:收入 2：支出
     * @param orderNo 订单编号
     * @param title 标题
     * @param type 流水类型 1:充值 2:购买优惠券 3:购买商品 4:优惠券退款 5:商品退款 6:注册赠送 7:邀请奖励 99:平台赠送
     * @return
    */
    @Override
    @Async("taskExecutor")
    @Transactional(rollbackFor = Exception.class)
    public void changeMoney(String userId, BigDecimal changeMoney, Integer billType, String orderNo, String title, int type) {
        if (changeMoney.signum() <= 0)
            return;
        Account account = this.queryByUserId(userId);
        if (ObjectUtil.isNull(account))
            return;
        accountBillService.crateBill(userId, changeMoney, billType, type, orderNo, title, account);
        //@create by FanJq 2024/12/12 10:23 这里修改金额使用单字段操作 避免脏读幻读数据写入
        super.baseMapper.changeMoney(userId, changeMoney, billType == 1 ? "+" : "-");
    }

    /**
     * @Description
     * 管理后台账户列表
     * @date 2025/1/2 14:12
     * @author FanJq
     * @param keywords
     * @return {@link List< AccountAdmin>}
    */
    @Override
    public List<AccountAdmin> getAdminList(String keywords) {
        return super.baseMapper.getAdminList(keywords);
    }

    /**
     * @Description
     * 更新账户余额
     * @date 2025/1/2 14:19
     * @author FanJq
     * @param id
     * @param money
     * @return
    */
    @Override
    public void modifyMoney(String id, BigDecimal money) {
        Account account = super.getById(id);
        int i = account.getMoney().compareTo(money);
        if (i == 0)
            return;
        if (i == 1) {
            //@create by FanJq 2025/1/2 14:23 资金减少
            BigDecimal change = account.getMoney().subtract(money);
            this.changeMoney(account.getUserId(), change, _Whole.TWO, "", "平台扣除", 98);
        } else if (i == -1) {
            //@create by FanJq 2025/1/2 14:23 资金增加
            BigDecimal change = money.subtract(account.getMoney());
            this.changeMoney(account.getUserId(), change, _Whole.ONE, "", "平台赠送", 99);
        }
    }
}
